# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import codecs
import json
import logging
import os

LOG = logging.getLogger(__name__)

CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_PATH = os.path.dirname(CURRENT_PATH)


class JsonWriterPipeline(object):
    file = None

    def open_spider(self, spider):
        LOG.debug('JsonWriterPipeline.open_spider: %s', spider.name)
        filepath = os.path.join(PROJECT_PATH, 'items-%s.txt' % spider.name)
        self.file = codecs.open(filepath, 'w', encoding='utf-8')

    def close_spider(self, spider):
        LOG.debug('JsonWriterPipeline.close_spider: %s', spider.name)
        self.file.close()

    def process_item(self, item, spider):
        LOG.debug('JsonWriterPipeline.process_item: %s', spider.name)
        line = json.dumps(dict(item), ensure_ascii=False) + '\n'
        self.file.write(line)
        return item
